package com.buildings.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.buildings.R;
import com.buildings.activity.ReadFileActivity;
import com.buildings.bean.FileSelectEntity;

import java.io.File;
import java.util.List;

public class FileListAdapter extends RecyclerView.Adapter<FileListAdapter.ViewHolder> {
    private Activity activity;
    private List<FileSelectEntity> files;
    private boolean isEdit = false;
    public FileListAdapter(Activity activity, List<FileSelectEntity> files) {
        this.activity = activity;
        this.files = files;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_file_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String path = files.get(position).getFile().getPath();
        String[] arr = path.split("/");
        String name = "";
        if (arr.length >0){
            name = arr[arr.length -1];
        }
        holder.tvName.setText(name);
        if (isEdit){
            holder.iv_select.setVisibility(View.VISIBLE);
        }else {
            holder.iv_select.setVisibility(View.GONE);
        }
        if (files.get(position).isSelect()){
            holder.iv_select.setImageResource(R.mipmap.icon_select_y);
        }else {
            holder.iv_select.setImageResource(R.mipmap.icon_select_n);
        }
        holder.iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener){
                    listener.onCheck(position);
                }
            }
        });
        holder.ll_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                    listener.onItemClick(position);
            }
        });
        holder.tv_usb_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener)
                    listener.onDelete(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == files ? 0 : files.size();
    }

    public void setIsEdit(boolean b) {
        this.isEdit = b;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        Button tv_usb_delete;//删除
        RelativeLayout ll_item;//绑定
        ImageView iv_select;//多选标志
        TextView tvName;//文件名
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_usb_delete = itemView.findViewById(R.id.tv_usb_delete);
            ll_item = itemView.findViewById(R.id.ll_item);
            iv_select = itemView.findViewById(R.id.iv_select);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
        void onCheck(int position);
        void onDelete(int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
